package java8.chapter08.test;

import java.util.ArrayList;
import java.util.List;

public class ObserverMain {

	private static class LeMonde implements Observer {

		@Override
		public void inform(String tweet) {
			if (tweet != null && tweet.contains("wine"))
				System.out.println("Today cheese, wine and news! " + tweet);
		}

	}

	private static class Guardian implements Observer {

		@Override
		public void inform(String tweet) {
			if (tweet != null && tweet.contains("queen"))
				System.out.println("Yet another news in London... " + tweet);
		}

	}

	private static class NYTimes implements Observer {

		@Override
		public void inform(String tweet) {
			if (tweet != null && tweet.contains("money"))
				System.out.println("Breaking news in NY!" + tweet);
		}

	}

	private static class Feed implements Subject {
		
		private final List<Observer> observers = new ArrayList<>();

		@Override
		public void registerObserver(Observer o) {
			this.observers.add(o);
		}

		@Override
		public void notifyObservers(String tweet) {
			observers.forEach(o -> o.inform(tweet));
		}

	}

	interface Observer {
		
		void inform(String tweet);
		
	}

	interface Subject {
		
		void registerObserver(Observer o);
		void notifyObservers(String tweet);
		
	}

	public static void main(String[] args) {
		Feed f = new Feed();
		f.registerObserver(new NYTimes());
		f.registerObserver(new Guardian());
		f.registerObserver(new LeMonde());
		f.notifyObservers("The queen said her favourite book is Java 8 in Action!");
	}

}